package com.cv.framework.common.exception;

import com.cv.framework.common.exception.constants.GlobalErrorCodeConstants;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serial;
import java.io.Serializable;

/**
 * 业务异常
 *
 * @author Charles_XDXD
 */
@Data
@EqualsAndHashCode(callSuper = true)
public final class ServiceException extends RuntimeException implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    /**
     * 业务错误码
     */
    private Integer code;

    /**
     * 错误提示，解决 Dubbo 序列化 message 失败的问题
     */
    private String errorMsg;

    /**
     * 错误提示
     */
    private String message;

    /**
     * 空构造方法，避免反序列化问题
     */
    public ServiceException() {
        super();
    }

    public ServiceException(ErrorCode errorCode) {
        this.code = errorCode.getCode();
        this.errorMsg = errorCode.getMsg();
        this.message = errorMsg;
    }

    public ServiceException(Integer code, String errorMsg) {
        this.code = code;
        this.errorMsg = errorMsg;
    }

    public ServiceException(String message) {
        this.message = message;
        this.code = GlobalErrorCodeConstants.BAD_REQUEST.getCode();
    }

    public ServiceException setCode(Integer code) {
        this.code = code;
        return this;
    }

    public ServiceException setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
        return this;
    }

    public ServiceException setMessage(String message) {
        this.message = this.errorMsg;
        return this;
    }

    public String getMessage() {
        return this.errorMsg;
    }

}
